using System;
using System.Collections.Generic;
using System.Drawing;

namespace StarSkipGame
{
    /// <summary>
    /// 游戏模式枚举
    /// </summary>
    public enum GameMode
    {
        Classic,        // 经典模式
        Survival,       // 生存模式
        TimeAttack,     // 时间挑战
        Endless,        // 无尽模式
        BossRush        // Boss挑战
    }

    /// <summary>
    /// 游戏模式配置类
    /// </summary>
    public class GameModeConfig
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public float InitialAsteroidSpawnInterval { get; set; }
        public float DifficultyIncreaseRate { get; set; }
        public float MaxDifficulty { get; set; }
        public bool HasTimeLimit { get; set; }
        public float TimeLimit { get; set; }
        public bool HasBossAsteroids { get; set; }
        public float BossSpawnInterval { get; set; }
        public int MaxAsteroids { get; set; }
        public int MinAsteroidsPerSpawn { get; set; }
        public int MaxAsteroidsPerSpawn { get; set; }
        public float PlayerSpeedMultiplier { get; set; }
        public bool HasPowerUps { get; set; }
        public Color ThemeColor { get; set; }

        public GameModeConfig()
        {
            Name = "Classic";
            Description = "经典躲避游戏模式";
            InitialAsteroidSpawnInterval = 3.0f;
            DifficultyIncreaseRate = 0.1f;
            MaxDifficulty = 1.0f;
            HasTimeLimit = false;
            TimeLimit = 0;
            HasBossAsteroids = false;
            BossSpawnInterval = 0;
            MaxAsteroids = 20;
            MinAsteroidsPerSpawn = 1;
            MaxAsteroidsPerSpawn = 3;
            PlayerSpeedMultiplier = 1.0f;
            HasPowerUps = false;
            ThemeColor = System.Drawing.Color.White;
        }
    }

    /// <summary>
    /// 游戏模式管理器类
    /// </summary>
    public class GameModeManager
    {
        private Dictionary<GameMode, GameModeConfig> gameModes;
        private GameMode currentMode;
        private GameModeConfig currentConfig;

        public GameMode CurrentMode
        {
            get { return currentMode; }
        }

        public GameModeConfig CurrentConfig
        {
            get { return currentConfig; }
        }

        public GameModeManager()
        {
            InitializeGameModes();
            SetGameMode(GameMode.Classic);
        }

        /// <summary>
        /// 初始化所有游戏模式
        /// </summary>
        private void InitializeGameModes()
        {
            gameModes = new Dictionary<GameMode, GameModeConfig>();

            // 经典模式
            gameModes[GameMode.Classic] = new GameModeConfig
            {
                Name = "经典模式",
                Description = "传统的星际躲避游戏，躲避陨石生存尽可能长的时间。",
                InitialAsteroidSpawnInterval = 1.2f,
                DifficultyIncreaseRate = 0.15f,
                MaxDifficulty = 2.0f,
                HasTimeLimit = false,
                TimeLimit = 0,
                HasBossAsteroids = false,
                BossSpawnInterval = 0,
                MaxAsteroids = 40,
                MinAsteroidsPerSpawn = 4,
                MaxAsteroidsPerSpawn = 10,
                PlayerSpeedMultiplier = 1.0f,
                HasPowerUps = false,
                ThemeColor = System.Drawing.Color.White
            };

            // 生存模式
            gameModes[GameMode.Survival] = new GameModeConfig
            {
                Name = "生存模式",
                Description = "高难度生存挑战，陨石生成更快，难度提升更迅速。",
                InitialAsteroidSpawnInterval = 0.8f,
                DifficultyIncreaseRate = 0.25f,
                MaxDifficulty = 3.0f,
                HasTimeLimit = false,
                TimeLimit = 0,
                HasBossAsteroids = false,
                BossSpawnInterval = 0,
                MaxAsteroids = 50,
                MinAsteroidsPerSpawn = 6,
                MaxAsteroidsPerSpawn = 15,
                PlayerSpeedMultiplier = 1.2f,
                HasPowerUps = true,
                ThemeColor = System.Drawing.Color.Red
            };

            // 时间挑战
            gameModes[GameMode.TimeAttack] = new GameModeConfig
            {
                Name = "时间挑战",
                Description = "在限定时间内生存，时间到后游戏结束。",
                InitialAsteroidSpawnInterval = 1.0f,
                DifficultyIncreaseRate = 0.2f,
                MaxDifficulty = 2.5f,
                HasTimeLimit = true,
                TimeLimit = 60.0f, // 60秒
                HasBossAsteroids = false,
                BossSpawnInterval = 0,
                MaxAsteroids = 45,
                MinAsteroidsPerSpawn = 5,
                MaxAsteroidsPerSpawn = 12,
                PlayerSpeedMultiplier = 1.1f,
                HasPowerUps = true,
                ThemeColor = System.Drawing.Color.Yellow
            };

            // 无尽模式
            gameModes[GameMode.Endless] = new GameModeConfig
            {
                Name = "无尽模式",
                Description = "无限时间的挑战，难度会持续增加直到无法生存。",
                InitialAsteroidSpawnInterval = 1.5f,
                DifficultyIncreaseRate = 0.1f,
                MaxDifficulty = 6.0f,
                HasTimeLimit = false,
                TimeLimit = 0,
                HasBossAsteroids = true,
                BossSpawnInterval = 30.0f, // 每30秒出现一个Boss
                MaxAsteroids = 60,
                MinAsteroidsPerSpawn = 8,
                MaxAsteroidsPerSpawn = 20,
                PlayerSpeedMultiplier = 1.0f,
                HasPowerUps = true,
                ThemeColor = System.Drawing.Color.Purple
            };

            // Boss挑战
            gameModes[GameMode.BossRush] = new GameModeConfig
            {
                Name = "Boss挑战",
                Description = "专门挑战大型Boss陨石，击败Boss获得高分。",
                InitialAsteroidSpawnInterval = 2.0f,
                DifficultyIncreaseRate = 0.15f,
                MaxDifficulty = 4.0f,
                HasTimeLimit = false,
                TimeLimit = 0,
                HasBossAsteroids = true,
                BossSpawnInterval = 15.0f, // 每15秒出现一个Boss
                MaxAsteroids = 25,
                MinAsteroidsPerSpawn = 3,
                MaxAsteroidsPerSpawn = 8,
                PlayerSpeedMultiplier = 1.3f,
                HasPowerUps = true,
                ThemeColor = System.Drawing.Color.Orange
            };
        }

        /// <summary>
        /// 设置游戏模式
        /// </summary>
        /// <param name="mode">游戏模式</param>
        public void SetGameMode(GameMode mode)
        {
            if (gameModes.ContainsKey(mode))
            {
                currentMode = mode;
                currentConfig = gameModes[mode];
            }
        }

        /// <summary>
        /// 获取指定模式的配置
        /// </summary>
        /// <param name="mode">游戏模式</param>
        /// <returns>模式配置</returns>
        public GameModeConfig GetGameModeConfig(GameMode mode)
        {
            if (gameModes.ContainsKey(mode))
            {
                return gameModes[mode];
            }
            return null;
        }

        /// <summary>
        /// 获取所有可用的游戏模式
        /// </summary>
        /// <returns>游戏模式列表</returns>
        public List<GameMode> GetAvailableGameModes()
        {
            return new List<GameMode>(gameModes.Keys);
        }

        /// <summary>
        /// 检查游戏是否应该结束（基于时间限制）
        /// </summary>
        /// <param name="gameTime">当前游戏时间</param>
        /// <returns>是否应该结束</returns>
        public bool ShouldEndGame(float gameTime)
        {
            if (currentConfig.HasTimeLimit && gameTime >= currentConfig.TimeLimit)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 计算当前难度下的陨石生成间隔
        /// </summary>
        /// <param name="gameTime">游戏时间</param>
        /// <returns>生成间隔</returns>
        public float GetAsteroidSpawnInterval(float gameTime)
        {
            float difficulty = Math.Min(currentConfig.MaxDifficulty, 
                gameTime * currentConfig.DifficultyIncreaseRate);
            
            return Math.Max(0.2f, 
                currentConfig.InitialAsteroidSpawnInterval - difficulty);
        }

        /// <summary>
        /// 检查是否应该生成Boss陨石
        /// </summary>
        /// <param name="gameTime">游戏时间</param>
        /// <param name="lastBossSpawnTime">上次Boss生成时间</param>
        /// <returns>是否应该生成Boss</returns>
        public bool ShouldSpawnBoss(float gameTime, float lastBossSpawnTime)
        {
            if (!currentConfig.HasBossAsteroids)
                return false;

            return (gameTime - lastBossSpawnTime) >= currentConfig.BossSpawnInterval;
        }

        /// <summary>
        /// 获取模式名称
        /// </summary>
        /// <param name="mode">游戏模式</param>
        /// <returns>模式名称</returns>
        public string GetModeName(GameMode mode)
        {
            var config = GetGameModeConfig(mode);
            return config != null ? config.Name : mode.ToString();
        }

        /// <summary>
        /// 获取模式描述
        /// </summary>
        /// <param name="mode">游戏模式</param>
        /// <returns>模式描述</returns>
        public string GetModeDescription(GameMode mode)
        {
            var config = GetGameModeConfig(mode);
            return config != null ? config.Description : "";
        }

        /// <summary>
        /// 获取模式主题颜色
        /// </summary>
        /// <param name="mode">游戏模式</param>
        /// <returns>主题颜色</returns>
        public System.Drawing.Color GetModeThemeColor(GameMode mode)
        {
            var config = GetGameModeConfig(mode);
            return config != null ? config.ThemeColor : System.Drawing.Color.White;
        }
    }
} 